﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace CoreShop.App
{
    public partial class frmFoodpackFixed : UIForm
    {
        private List<corecmsfoodordering> packdetailList = null;
        public corecmsfoodordering orderingParent;
        private int minSelect = 0;
        private int maxSelect = 0;

        public frmFoodpackFixed()
        {
            InitializeComponent();
        }

        private void frmfoodpack_Load(object sender, EventArgs e)
        {
            lblFoodName.Text = "套菜名称：" + orderingParent.name;
            var packList = new corecmsfoodpackServices().GetListByFoodId(orderingParent.foodId);
            packdetailList = new corecmsfoodorderingServices().GetListByFoodPackId(orderingParent);
            if (packdetailList.Count == 0)
            {
                ShowInfoTip("没有套菜明细,无法选择。");
                btnOk.Visible = false;
                return;
            }

            string packGroupName = "";
            foreach (var item in packList)
            {
                minSelect += item.quantityStar;
                maxSelect += item.quantityEnd;
                string sSelectType = EnumHelper.GetDescByValue<GlobalEnumVars.FoodPackSelectType>(item.selectType);
                string packName = item.name;
                if (string.IsNullOrEmpty(packName)) packName = "不定";

                packGroupName = string.Format("{0} ---(说明:{1},选择{2}~{3}项)", packName, sSelectType, item.quantityStar, item.quantityEnd);
                if (string.IsNullOrEmpty(packGroupName)) packGroupName = item.id.ToString();
                AddType(packGroupName);
                var packGroup = packdetailList.Where(p => p.packGroupId == item.id).ToList();
                int rows = CommFunc.ConvertToInt32(Math.Ceiling(packGroup.Count / 5.0));
                AddItems(packGroupName, packGroup, false, new Size(140, 40), 50 * rows, item.quantityEnd);
            }
        }

        public void AddType(string type)
        {
            UIMenuItemSeparator item = new UIMenuItemSeparator(type, 40, 500);
            item.SetOtherMode();
            layTaskList.Add(item);
        }

        public void AddItems(string type, List<corecmsfoodordering> items, bool autoWidth, Size itemSize, int height, int maxSelected = int.MaxValue)
        {
            UIMultiButtons buttons = new UIMultiButtons();
            buttons.Width = layTaskList.Width - 30;
            buttons.Height = height;
            buttons.TagString = type;
            buttons.AutoWidth = autoWidth;
            buttons.MaxSelected = maxSelected;
            buttons.ItemSize = itemSize;
            buttons.OnOverSelectedCount += Buttons_OnOverSelectedCount;
            bool selectFlag = false;
            if (maxSelected == items.Count) selectFlag = true;
            foreach (var item in items)
            {
                buttons.Add(item.name, item.rowId, selectFlag);
            }

            layTaskList.Add(buttons);
        }

        private void Buttons_OnOverSelectedCount(object sender, System.EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            string tagString = button.TagString;
            int index = tagString.IndexOf("---");
            tagString = tagString.Substring(0, index) + "只能最多选择" + button.MaxSelected + "项";
            UIMessageTip.ShowWarning(button, tagString);
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            orderingParent.sourceId = (long)YitIdHelper.NextId();
            var foodorderingList = new List<corecmsfoodordering> { orderingParent };
            var buttons = this.GetControls<UIMultiButtons>(true);
            int rowId = orderingParent.rowId;
            foreach (UIMultiButtons button in buttons)
            {
                long[] foodIds = button.SelectedButtonsValues;
                var foodSels = packdetailList.Where(p => foodIds.Contains(p.rowId)).ToList();
                foreach (var item in foodSels)
                {
                    rowId++;
                    item.orderId = orderingParent.orderId;
                    item.sourceId = orderingParent.sourceId;
                    item.billerId = AppConstVars.sysUser.id;
                    item.itemCount = 1;
                    item.rowId = rowId;
                    foodorderingList.Add(item);
                }
            }

            if (foodorderingList.Count > maxSelect + 1)
            {
                string msg = string.Format("无法提交，最多只能选择{0}个商品。", maxSelect);
                UIMessageBox.Show(msg, "系统提示", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            if (foodorderingList.Count < minSelect + 1)
            {
                string msg = string.Format("最少选择{0}个商品，目前只选择{1}个商品，确定要提交吗？。", maxSelect, foodorderingList.Count - 1);
                if (!ShowAskDialog(msg)) return;
            }
            var bl = new corecmsfoodorderingServices().InsertRange(foodorderingList);
            if (!bl)
            {
                UIMessageBox.Show("保存失败。", "系统出错-SaveOrderingFeed:", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }            

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}